package cn.jasonone.him.model;

import java.util.HashMap;

import lombok.Data;
import lombok.experimental.Accessors;

/**
 * 消息结果封装对象
 * 
 * @author Jason
 *
 */
@Data
@Accessors(chain = true)
public class R extends HashMap<String, Object> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private R parent;

	private R root;

	public static R success() {
		return new R().setCode(0);
	}

	public static R error(int code, String msg) {
		return new R().setCode(code).setMsg(msg);
	}

	public R createChild(String key) {
		R r = new R();
		r.parent = this;
		r.root = this.root == null ? this : this.root;
		this.put(key, r);
		return r;
	}

	@SuppressWarnings("all")
	public <T> T get(String key) {
		return (T) super.get(key);
	}

	public R setCode(int code) {
		this.put("code", code);
		return this;
	}

	public int getCode() {
		return this.get("code");
	}

	public R setMsg(String msg) {
		this.put("msg", msg);
		return this;
	}

	public String getMsg() {
		return this.get("msg");
	}

	public R addItem(String key, Object value) {
		this.put(key, value);
		return this;
	}
}
